package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import java.util.ArrayList;

import javax.crypto.spec.PSource;

import www.openteam.com.R;
import www.openteam.com.bean.OrderApprovalitem;

/**
 * Created by OT on 2017-10-09.
 */

public class CustomChildAdapter extends RecyclerView.Adapter<CustomChildAdapter.ViewHodler> {
    Context context;
    ArrayList<OrderApprovalitem> orderApprovalitemList;
    OnItemClickListener onItemClickListener;
    boolean isInspect;

    public interface OnItemClickListener{
        void onClickModify(int postion);
        void onClickDetele(int postion);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public CustomChildAdapter(Context context, ArrayList<OrderApprovalitem> orderApprovalitemList, boolean isInspect) {
        this.context = context;
        this.orderApprovalitemList = orderApprovalitemList;
        this.isInspect = isInspect;
    }

    @Override
    public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_corder_detailed, parent, false);
        return new ViewHodler(view);
    }

    @Override
    public void onBindViewHolder(ViewHodler holder, final int position) {
        OrderApprovalitem cuss = orderApprovalitemList.get(position);
        holder.dproductno.setText(cuss.getProdno());
        holder.dproductname.setText(cuss.getProdname());
        holder.dproductprice.setText(cuss.getPrice() + "\t*\t" + cuss.getPaynumb() + "\t=\t" + cuss.getPrice() * cuss.getPaynumb());
        holder.paperinf.setText(cuss.getPaperinf());
        holder.wraptype.setText(cuss.getWraptype());
        holder.dsenddate.setText(cuss.getSenddate());
        holder.printcolor.setText(cuss.getPrintcolor());
        holder.technicsinf.setText(cuss.getTechnicsinf());
        holder.onum.setText(cuss.getOthernumb() + "");
        holder.dproductnumb.setText(cuss.getPaynumb() + "");
        if (isInspect){
            holder.btn_modify_detailed.setVisibility(View.INVISIBLE);
            holder.btn_delete_detailed.setVisibility(View.INVISIBLE);
        }else {
            holder.btn_modify_detailed.setVisibility(View.VISIBLE);
            holder.btn_delete_detailed.setVisibility(View.VISIBLE);
            if (onItemClickListener!=null){
                holder.btn_modify_detailed.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onClickModify(position);
                    }
                });
                holder.btn_delete_detailed.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onClickDetele(position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return orderApprovalitemList.size();
    }

    public class ViewHodler extends RecyclerView.ViewHolder {
        private int id = -1;
        private TextView dproductname;
        private TextView dproductno;
        private TextView dproductprice, printcolor, technicsinf,
                paperinf, wraptype, dsenddate,
                onum, dproductnumb;

        Button btn_modify_detailed, btn_delete_detailed;

        public ViewHodler(View itemView) {
            super(itemView);
            dproductname = (TextView) itemView.findViewById(R.id.tv_order_dproductname);
            dproductno = (TextView) itemView.findViewById(R.id.tv_order_dproductno);
            dproductprice = (TextView) itemView.findViewById(R.id.tv_order_dproductprice);
            printcolor = (TextView) itemView.findViewById(R.id.tv_order_printcolor);
            technicsinf = (TextView) itemView.findViewById(R.id.tv_order_technicsinf);
            paperinf = (TextView) itemView.findViewById(R.id.tv_order_paperinf);
            wraptype = (TextView) itemView.findViewById(R.id.tv_order_wraptype);
            dsenddate = (TextView) itemView.findViewById(R.id.tv_order_dsenddate);
            onum = (TextView) itemView.findViewById(R.id.tv_order_onum);
            dproductnumb = (TextView) itemView.findViewById(R.id.tv_order_dproductnumb);
            btn_modify_detailed = (Button) itemView.findViewById(R.id.btn_modify_detailed);
            btn_delete_detailed = (Button) itemView.findViewById(R.id.btn_delete_detailed);
        }
    }
}
